package com.yunti.business.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.yunti.business.domain.BaseSubItem;
import com.yunti.business.domain.SubjectTree;
import com.yunti.business.mapper.BaseSubItemMapper;
import com.yunti.business.service.IBaseSubItemService;
import com.yunti.business.vo.res.BaseSubItemWithScoreRes;
import com.yunti.business.vo.res.SubjectWithItem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunti.business.mapper.BaseSubjectMapper;
import com.yunti.business.domain.BaseSubject;
import com.yunti.business.service.IBaseSubjectService;

/**
 * 项目管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-20
 */
@Service
public class BaseSubjectServiceImpl implements IBaseSubjectService {
    @Autowired
    private BaseSubjectMapper baseSubjectMapper;

    @Autowired
    private BaseSubItemMapper baseSubItemMapper;

    /**
     * 查询项目管理
     *
     * @param id 项目管理主键
     * @return 项目管理
     */
    @Override
    public BaseSubject selectBaseSubjectById(Long id) {
        return baseSubjectMapper.selectBaseSubjectById(id);
    }

    /**
     * 查询项目管理列表
     *
     * @param baseSubject 项目管理
     * @return 项目管理
     */
    @Override
    public List<BaseSubject> selectBaseSubjectList(BaseSubject baseSubject) {
        return baseSubjectMapper.selectBaseSubjectList(baseSubject);
    }

    /**
     * 新增项目管理
     *
     * @param baseSubject 项目管理
     * @return 结果
     */
    @Override
    public int insertBaseSubject(BaseSubject baseSubject) {
        baseSubject.setCreateTime(LocalDateTime.now());
        return baseSubjectMapper.insertBaseSubject(baseSubject);
    }

    /**
     * 修改项目管理
     *
     * @param baseSubject 项目管理
     * @return 结果
     */
    @Override
    public int updateBaseSubject(BaseSubject baseSubject) {
        baseSubject.setUpdateTime(LocalDateTime.now());
        return baseSubjectMapper.updateBaseSubject(baseSubject);
    }

    /**
     * 批量删除项目管理
     *
     * @param ids 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    public int deleteBaseSubjectByIds(Long[] ids) {
        return baseSubjectMapper.deleteBaseSubjectByIds(ids);
    }

    /**
     * 删除项目管理信息
     *
     * @param id 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteBaseSubjectById(Long id) {
        return baseSubjectMapper.deleteBaseSubjectById(id);
    }

    @Override
    public List<SubjectTree> selectDeptTreeList(BaseSubject baseSubject) {
        List<BaseSubject> baseSubjectList = baseSubjectMapper.selectBaseSubjectList(baseSubject);
        return buildDeptTreeSelect(baseSubjectList);
    }


    /**
     * 构建前端所需要下拉树结构
     *
     * @param baseSubjectList 部门列表
     * @return 下拉树结构列表
     */
    public List<SubjectTree> buildDeptTreeSelect(List<BaseSubject> baseSubjectList) {
        List<BaseSubject> deptTrees = buildDeptTree(baseSubjectList);
        return deptTrees.stream().map(SubjectTree::new).collect(Collectors.toList());
    }

    public List<BaseSubject> buildDeptTree(List<BaseSubject> baseSubjectList) {
        List<BaseSubject> returnList = new ArrayList<BaseSubject>();
        List<Long> tempList = baseSubjectList.stream().map(BaseSubject::getId).collect(Collectors.toList());
        for (BaseSubject baseSubject : baseSubjectList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(baseSubject.getParentId())) {
                recursionFn(baseSubjectList, baseSubject);
                returnList.add(baseSubject);
            }
        }
        if (returnList.isEmpty()) {
            returnList = baseSubjectList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<BaseSubject> list, BaseSubject t) {
        // 得到子节点列表
        List<BaseSubject> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseSubject tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BaseSubject> getChildList(List<BaseSubject> list, BaseSubject t) {
        List<BaseSubject> tlist = new ArrayList<BaseSubject>();
        Iterator<BaseSubject> it = list.iterator();
        while (it.hasNext()) {
            BaseSubject n = (BaseSubject) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BaseSubject> list, BaseSubject t) {
        return getChildList(list, t).size() > 0;
    }

    @Override
    public List<SubjectWithItem> selectSubjectWithItemByIdList(List<Long> subIdList) {
        LambdaQueryWrapper<BaseSubject> baseSubjectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseSubjectLambdaQueryWrapper.in(BaseSubject::getId, subIdList);
        List<BaseSubject> baseSubjectList = baseSubjectMapper.selectList(baseSubjectLambdaQueryWrapper);

        LambdaQueryWrapper<BaseSubItem> baseSubItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseSubItemLambdaQueryWrapper.in(BaseSubItem::getSubId, subIdList);
        List<BaseSubItem> baseSubItemList = baseSubItemMapper.selectList(baseSubItemLambdaQueryWrapper);
        List<BaseSubItemWithScoreRes> baseSubItemWithScoreResArrayList = new ArrayList<>();
        for (int i = 0; i < baseSubItemList.size(); i++) {
            BaseSubItemWithScoreRes baseSubItemWithScoreRes = new BaseSubItemWithScoreRes();
            BeanUtils.copyProperties(baseSubItemList.get(i),baseSubItemWithScoreRes);
            baseSubItemWithScoreResArrayList.add(baseSubItemWithScoreRes);
        }

        Map<Long, List<BaseSubItemWithScoreRes>> subItemMap = baseSubItemWithScoreResArrayList.stream().collect(Collectors.groupingBy(BaseSubItem::getSubId));
        List<SubjectWithItem> subjectWithItemList = new ArrayList<>();
        for (int i = 0; i < baseSubjectList.size(); i++) {
            SubjectWithItem subjectWithItem = new SubjectWithItem();
            BeanUtils.copyProperties(baseSubjectList.get(i), subjectWithItem);
            subjectWithItem.setBaseSubItemList(subItemMap.get(baseSubjectList.get(i).getId()));


            subjectWithItemList.add(subjectWithItem);

        }
        return subjectWithItemList;
    }
}
